<?php
/*=============================================================================
|| ##################################################################
||	phpFoX Konsort
|| ##################################################################
||
||	Copyright		: (C) 2005-2007 PHPFOX Limited a Reality Fox Creation
||	Contact			: info@phpfox.com
||
||	- phpFoX and all of its source code and files are protected by Copyright Laws.
||
||	- The license for phpFoX permits you to install this software on a single domain (.com, .org, .net, etc.).
||
||	- You may also not remove this copyright screen which shows the copyright information and credits for phpFoX (RealityFOX Creations).
||
||	- phpFoX Konsort is NOT a FREE software - http://www.phpfox.com/license/
||
|| ##################################################################
=============================================================================*/

/* $File: ./include/modules/Event/classes/PhpFox_ComponentInvite.class.php, $Author:  $ */



/** Component for inviting users to events
 * @version $Id: PhpFox_ComponentInvite.class.php 46 2007-12-14 23:12:21Z phpfoxsv $
 * @package module.event
 *
 */
class PhpFox_Mod_Event_ComponentInvite extends BaseComponent
{
    function process()
    {
    	App::membersOnly();

        $oFriendsService = &App::getModuleService('Account', 'Friends');
		/* @var $oFriendsService PhpFox_Mod_Account_ServiceFriends */
		$oEventItem = &$this->_oModule->getItem('Event');
        /* @var $oEventItem PhpFox_Mod_Event_ItemEvent */
        $oSecurityService = &App::getModuleService('Account', 'Security');
        /* @var $oSecurityService PhpFox_Mod_Account_ServiceSecurity */
        $oEventService = &$this->_oModule->getService('Event');
        /* @var $oEventService PhpFox_Mod_Event_ServiceEvent */
        $oEventInviteItem = &$this->_oModule->getItem('EventInvite');
        /* @var $oEventInviteItem PhpFox_Mod_Event_ItemEventInvite */
        $oTpl = &$this->_oModule->getTemplate();
        $oReq = &App::getRequest();

        $oSecurityService->checkPerm('events', 'full');
        $oSecurityService->checkAccess('events');
        $iEventId = $oReq->getInt('id');
        $bIsStaff = $oSecurityService->isStaff();
        $bIsAdmin = $oSecurityService->isAdmin();
        $oCurrUser = $oSecurityService->getCurrentUser();
        $sCurrentUser = $oCurrUser->aData['user'];
        if (!$iEventId)
        {
            App::gotoUrl('public.event.main');
        }
        if (!$oEventItem->load($iEventId))
        {
            App::gotoUrl('public.event.main');
        }
        if (($oEventItem->aData['user'] != $sCurrentUser) && !$bIsAdmin)
        {
            App::gotoUrl('public.event.main');
        }
        if ($oReq->getAction() == 'invite')
        {
            $aUsers = $oReq->getArray('listRight');
            if (!count($aUsers))
            {
                App::gotoUrl('public.event.invite', array('id' => $iEventId), App::format('Mod_Event.error.no_selected'), true, 'invite');
            }
            foreach ($aUsers as $sUser)
            {
                $oEventInviteItem->aData = array
                (
                	'event_id' => $iEventId,
                	'user'     => $sUser,
                	'time'     => time(),
                );
                $oEventInviteItem->insert();
            }
            $oTpl->assign(array(
            	'bSend'  => true,
            	'aUsers' => $aUsers,
            ));
            return $oTpl->fetch('InviteForm.html');
        }
        $sSort = 'time DESC';
        list($aFriends) = $oFriendsService->getFriendsList($oCurrUser, 0, 0, $sSort);
        $aCond = array();
        $sSort = 'ei.time DESC';
        $aFields = array
        (
        	'user' => 'user',
        );
        $aCond['invite']   = '= '.( (isset($sMode) and $sMode == 'attended') ? 1 : 0);
        $aCond['type']     = '= 0';
        $aCond['id']       = '= '.$iEventId;
        list($aInvites) = $oEventService->getEventInvites($aCond, 0, 0, '', $aFields);
        $aInviteList = array();
        foreach ($aInvites as $aInvite)
        {
            $aInviteList[] = $aInvite['user'];
        }
        foreach ($aFriends as $iKey => $aFriend)
        {
            if (in_array($aFriend['user'], $aInviteList))
            {
                unset($aFriends[$iKey]);
            }
        }

        $oTpl->assign(array(
        	'iEventId'   => $iEventId,
        	'aItems'     => $aFriends,
        	'sUImageUrl' => App::getParam('user.pic.url'),
        ));
        return $oTpl->fetch('InviteForm.html');
    }
}
?>